package com.yuan.socket.server;

import com.yuan.socket.data.DataService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.http.nio.protocol.Pipelined;

import java.util.concurrent.TimeUnit;

public class WebSocketServerInitializer extends ChannelInitializer {
    DataService dataServices;
    public WebSocketServerInitializer(DataService dataService) {
        dataServices = dataService;
    }
    @Override
    protected void initChannel(Channel channel) throws Exception {
        ChannelPipeline pipeline =  channel.pipeline();
        pipeline.addLast(new HttpServerCodec());   // HTTP编解码器
        pipeline.addLast(new HttpObjectAggregator(65536)); // 聚合HTTP消息
        pipeline.addLast(new WebSocketServerHandler(dataServices));
        pipeline.addLast("ping", new IdleStateHandler(720, 720, 720,
                TimeUnit.MINUTES));

    }
}
